package bandidong.bean;

import bandidong.dao.CategoryDAO;
import bandidong.dao.impl.CategoryDAOImpl;
import bandidong.model.Category;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
public class CategoryBean implements CategoryBeanRemote {
    
    @PersistenceContext(unitName = "persistenceUnit")
    EntityManager em;
    
    private CategoryDAO categoryDAO = null;
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Category> getAllCategories() {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.getAllCategories();
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Category getCategoryById(int categoryId) {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.getCategoryById(categoryId);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Category> getAllCategories(int page, int pageSize) {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.getAllCategories(page, pageSize);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String addCategory(Category category) {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.addCategory(category);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String updateCategory(Category category) {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.updateCategory(category);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String deleteCategory(Category category) {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.deleteCategory(category);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Category getCategoryByName(String categoryName) {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.getCategoryByName(categoryName);
    }
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkExists(String categoryName, int categoryId) {
        categoryDAO = new CategoryDAOImpl(em);
        return categoryDAO.checkExists(categoryName, categoryId);
    }
}
